﻿using System;
using System.Linq;
using System.IO;
using System.IO.IsolatedStorage;
using System.Collections.Generic;
using Microsoft.LightSwitch;
using Microsoft.LightSwitch.Framework.Client;
using Microsoft.LightSwitch.Presentation;
using Microsoft.LightSwitch.Presentation.Extensions;
namespace LightSwitchApplication
{
    public partial class SearchFilteredAddresses
    {
        partial void SearchFilteredAddresses_InitializeDataWorkspace(List<IDataService> saveChangesTo)
        {
            if (this.Application.User.HasPermission(Permissions.CenEditAddress))//admin
            {
            }
            else if (this.Application.User.HasPermission(Permissions.CanSearchAddress) &&
                    (this.Application.User.HasPermission(Permissions.CanManageMyShop) 
                        || this.Application.User.HasPermission(Permissions.CanManageMySalesOrder) 
                        || this.Application.User.HasPermission(Permissions.CanManageMyVisit))
                )
            {
                string userName = this.Application.User.FullName;
                //sales person
                foreach (SalesPerson item in this.DataWorkspace.ApplicationData.SalesPersons)
                {
                    if (item.aspnet_Profile != null && item.aspnet_Profile.PropertyValuesString == userName)
                    {
                        this.SalesPersonId = item.Id;
                        break;
                    }
                }

                //customer
                foreach (Customer item in this.DataWorkspace.ApplicationData.Customers.Where(c => c.aspnet_Profile_UserId != null))
                {
                    if (item.aspnet_Profile != null && item.aspnet_Profile.PropertyValuesString == userName)
                    {
                        this.CustomerId = item.Id;
                        break;
                    }
                }
            }
        }

        partial void Cancel_Execute()
        {
            this.Close(false);
        }

        partial void SearchFilteredAddresses_Activated()
        {
            // Write your code here.

        }
    }
}
